// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var constant = require("constant");
	var router = require("../hash/pageRouter");
	var tools = require("tools");
	var fileUuid = [];
	var css_product = require("./constractSupervisionCharge.css");
	var html_add_charge = require("./constractSupervisionCharge_add.html");
	var productId;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function(page) {
		productId = page.params.productId;
		inquireFinanceProductExpensePageList();
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#supervisionComplete").click(function() {
			router.route("constractSupervisionManage/constractSupervisionManage.html");
		});

		$("#constractSupervisionCharge").on("click", ".chargeDelete", function() {
			deleteFinanceProductExpense($(this).parents("tr").attr("productExpenseId"));
		});
		
		$("#supervisionBack").unbind("click").click(function(){
			router.route("constractSupervisionManage/constractSupervisionAdd.html?productId="+productId);
		});

		$("#addSuperversionChargeBtn").on("click", function() {
			$("#popdialog").html(html_add_charge);
			dialog.showDialog($("#add_supervision_dialog"));
			$(".dialogTitle").html("费用项目");
			autoComplete1();
			$("#constractType").html('<option value="3">项目费用</option>');
			$(".productId").val(productId);

			$("#supervisionCancel").unbind("click").click(function() {
				dialog.hideDialog($("#add_supervision_dialog"));
			});
			$("#supervisionSure").unbind("click").click(function() {
				databind.submitDom($(".chargeType2"), function() {
					base.alert(1, "费用添加成功！");
					dialog.hideDialog($("#add_supervision_dialog"));
					inquireFinanceProductExpensePageList();
				});
			});
			
			tools.inquireCommonSelectorListByName("费用名称", $("select[name=expenseName]"))
		});
	};

	var autoComplete = function(bulkstockCategoryId) {
		//自动补全
		$("#chargeSupervisionBulkstockName").autocomplete(seajs.data.handler + "inquireBulkstockPageList.json", {
			minChars: 1,
			max: 10,
			width: 120,
			matchCase: false, //不区分大小写
			scroll: true,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 1000,
				appKey: base.getData("appKey"),
				sessionUuid: base.getData("sid"),
				bulkstockCategoryId:bulkstockCategoryId
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {
				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.bulkstockName, //此处无需把全部列列出来，只是两个关键列
						result: row.bulkstockName
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#chargeSupervisionBulkstockName").hasClass('nonemorelist')) {
					$("#chargeSupervisionBulkstockName").removeClass('nonemorelist');
					return;
				};
				return "<table><tr><td>" + data.bulkstockName + "(" + data.specificationName + ")" + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.bulkstockName;
			},
			formatResult: function(data, value) {
				return data.bulkstockName;
			}
		}).result(function(event, data, formatted) { //回调
			$('#chargeSupervisionBulkstockName').val(data.bulkstockName);
			if ($("#chargeSupervisionBulkstockName").hasClass('nonemorelist')) {
				$("#chargeSupervisionBulkstockName").removeClass('nonemorelist');
			} else {
				$("#chargeSupervisionBulkstockName").addClass('nonemorelist');
			};
			$("#chargeSupervisionBulkstockId").val(data.bulkstockInfoId);
		});
	};

	var autoComplete1 = function() {
		//自动补全
		$("#chargeSupervisionCategoryName").autocomplete(seajs.data.handler + "inquireBulkstockCategoryPageListByKeyword.json", {
			minChars: 1,
			max: 10,
			width: 120,
			matchCase: false, //不区分大小写
			scroll: true,
			dataType: 'json',
			scrollHeight: 500,
			//此处为传递而外的参数
			extraParams: {
				pageSize: 1000,
				appKey: base.getData("appKey"),
				sessionUuid: base.getData("sid")
			},
			//需要把data转换成json数据格式                      
			parse: function(data) {
				return $.map(eval(data.datas), function(row) {
					return {
						data: row,
						value: row.categoryName, //此处无需把全部列列出来，只是两个关键列
						result: row.categoryName
					}
				});
			},
			formatItem: function(data, i, total) {
				if ($("#chargeSupervisionCategoryName").hasClass('nonemorelist')) {
					$("#chargeSupervisionCategoryName").removeClass('nonemorelist');
					return;
				};
				return "<table><tr><td>" + data.categoryName + "</td></tr></table>";
			},
			formatMatch: function(data, i, total) {
				return data.categoryName;
			},
			formatResult: function(data, value) {
				return data.categoryName;
			}
		}).result(function(event, data, formatted) { //回调
			$('#chargeSupervisionCategoryName').val(data.categoryName);
			if ($("#chargeSupervisionCategoryName").hasClass('nonemorelist')) {
				$("#chargeSupervisionCategoryName").removeClass('nonemorelist');
			} else {
				$("#chargeSupervisionCategoryName").addClass('nonemorelist');
			};
			$("#bulkstockSupervisionCategoryId").val(data.bulkstockCategoryId);
			autoComplete(data.bulkstockCategoryId);
		});
	};

	// 查询产品费用列表
	var inquireFinanceProductExpensePageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductExpensePageList.json", //接口名称
			oData: {
				productId: productId,
				pageSize: pageSize,
				pageIndex: pageIndex
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#supervisionListTpl").html();
					var html = juicer(tpl, data);
					$("#supervisionListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#supervisionPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceProductExpensePageList();
						});
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//删除商品费用
	var deleteFinanceProductExpense = function(productExpenseId) {
		base.fnAjax({
			action: "deleteFinanceProductExpense.json", //接口名称
			oData: {
				productExpenseId: productExpenseId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "费用删除成功!");
					inquireFinanceProductExpensePageList();
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});